﻿using System;
using System.Collections.Generic;

namespace 泛型集合练习
{
    class Program
    {
        public static Dictionary<int, Student> list = new Dictionary<int, Student>();

        static void Main(string[] args)
        {
            list.Add(1, new Student(001, "张三", 20));
            list.Add(2, new Student(002, "赵四", 19));
            list.Add(3,new Student(003, "王五", 28));

            while (true)
            {
                Console.WriteLine("1、添加学生信息 2、查询学生信息 3、删除学生信息");
                int key = int.Parse(Console.ReadLine());
                switch (key)
                {
                    case 1:
                        //Console.WriteLine("添加学生信息");
                        addStudent();
                        break;
                    case 2:
                        //Console.WriteLine("查询学生信息");
                        checkStudent();
                        break;
                    case 3:
                        //Console.WriteLine("删除学生信息");
                        removeStudent();
                        break;
                    case 4:
                        Console.WriteLine("感谢使用");
                        return;
                    default:
                        Console.WriteLine("输入错误，请重新输入");
                        break;
                }
            }
        }

        //删除学生信息
        private static void removeStudent()
        {
            Console.WriteLine("请输入学生学号");
            int num = int.Parse(Console.ReadLine());
            if (!list.ContainsKey(num))
            {
                Console.WriteLine("学号不存在，请重新输入");
                return;
            }
            list.Remove(num);
            Console.WriteLine("删除成功！");
        }

        //查询学生信息
        private static void checkStudent()
        {
            while (true)
            {
                Console.WriteLine("请选择：1、查询所有（按学号排序），2、查询所有（按姓名排序），3、查询所有（按年龄排序），4、按学号查询 ，5、退出");
                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        //Console.WriteLine("查询所有（按学号排序）");
                        checkAllStudentByNum();
                        break;
                    case "2":
                        // Console.WriteLine("查询所有（按姓名排序）");
                        checkAllStudentByName();
                        break;
                    case "3":
                        //Console.WriteLine("查询所有（按年龄排序）");
                        checkAllStudentByAge();
                        break;
                    case "4":
                        //Console.WriteLine("按学号查询");
                        checkStudentByNum();
                        break;
                    case "5":
                        Console.WriteLine("感谢使用查询功能，再见!");
                        return;
                    default:
                        Console.WriteLine("输入错误，请重新输入");
                        break;
                }
            }
        }
        //
        private static void checkStudentByNum()
        {
            Console.WriteLine("请输入学生学号");
            int num = int.Parse(Console.ReadLine());
            if (!list.ContainsKey(num))
            {
                Console.WriteLine("查无此学生");
                return;
            }
            else
            {
                Console.WriteLine(list[num]);
            }
        }
        //查询所有（按年龄排序）
        private static void checkAllStudentByAge()
        {
            List<Student> alist = new List<Student>();
            foreach (var value in list.Values)
            {
                alist.Add(value);
            }
            alist.Sort(new Age());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }
        //查询所有（按姓名排序）
        private static void checkAllStudentByName()
        {
            List<Student> alist = new List<Student>();
            foreach (var value in list.Values)
            {
                alist.Add(value);
            }
            alist.Sort(new Name());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }

        //查询所有（按学号排序）
        private static void checkAllStudentByNum()
        {
            Console.WriteLine("请输入学生学号");
            int num = int.Parse(Console.ReadLine());
            if (!list.ContainsKey(num))
            {
                Console.WriteLine("查无此学生");
                return;
            }
            else
            {
                Console.WriteLine(list[num]);
            }
        }

        //添加学生信息
        private static void addStudent()
        {
            int num;
            while (true)
            {
                Console.WriteLine("请输入学生学号");
                num = int.Parse(Console.ReadLine());
                if (list.ContainsKey(num))
                {
                    Console.WriteLine("学号已存在，请重新输入");
                    continue;
                }
                break;
            }
            Console.WriteLine("请输入学生姓名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入学生年龄");
            int age = int.Parse(Console.ReadLine());
            Student stu = new Student(num, name, age);
            list.Add(num, stu);
            Console.WriteLine("添加成功!");
        }
    }
}
